let nums15=[-1,0,1,2,-1,-4,-2,-3,3,0,4]
console.log(threeSum(nums15));

function threeSum(nums: number[]): number[][] {
    let result:[number,number,number][]=[]
    //1、先排序;a
    nums.sort((a, b) => a - b);
    
    let i=0;
    while(i<nums.length-2){
        if(nums[i]>0)
            break;
        let l=i+1,r=nums.length-1;
        while(l<r){
            if(nums[i]+nums[l]+nums[r]===0){
                result.push([nums[i],nums[l],nums[r]])
                l=moveL(l,nums);
                r=moveR(r,nums);
            }else if(nums[i]+nums[l]+nums[r]>0){
                r=moveR(r,nums);
            }else{  //<0
                l=moveL(l,nums);
            }
        }
        while(i<nums.length-2){
            if(nums[i]===nums[i+1])
                i++;
            else
                break;
        }
        i++;
    }
    return result;    
};
function moveR(r:number,nums:number[]):number{
    if(r-1===0)
        return 0;
    while((nums[r]===nums[r-1])){
       r--; 
       if(r<=1)
        break
    }
    r--;
    return r;
}
function moveL(l:number,nums:number[]):number{
    if(l+1===nums.length-1)
        return nums.length-1;
    while((nums[l]===nums[l+1])){
        l++;
        if(l>=nums.length-1)
            break;
    }
    l++;
    return l;
}